/**
 * 作者:hanbo
 * 日期:2018/12/2710:09
 * 描述:
 */
define([
        "dojo/_base/declare",
        "esri/widgets/DistanceMeasurement2D",
        "esri/widgets/AreaMeasurement2D",
        "esri/widgets/LayerList",
        "esri/widgets/Home",
        "esri/Viewpoint",
        "esri/geometry/Extent",
        "esri/widgets/ScaleBar",
        "esri/widgets/Print",
        "dojo/on",
        "dojo/dom"
    ],
    function (declare,DistanceMeasurement2D,AreaMeasurement2D, LayerList, Home, Viewpoint, Extent, ScaleBar, Print, on, dom) {
        return declare("UIControl", null, {
            constructor: function (view) {
                this.view = view;
                // var layerList = new LayerList({
                //     view: view
                // });
                // 图层控件
                // view.ui.add(layerList, "top-right");
                //移除底部文字
                view.ui.remove("attribution");
                var homeButton = new Home({
                    view: view,
                    viewpoint: new Viewpoint({
                        targetGeometry: new Extent({
                            xmin: 103.300136976,
                            ymin: 30.3538426998,
                            xmax: 103.342034525,
                            ymax: 30.3811850862,
                            spatialReference: {
                                wkid: 4326
                            }
                        })
                    })
                });
                view.ui.add(homeButton, "top-left");


                var clear = document.createElement("div");
                var i = document.createElement("i");
                i.className = "iconfont icon-qingchu";
                clear.appendChild(i);
                clear.id = "clearBtn";
                view.ui.add(clear, "top-left");
                on(dom.byId("clearBtn"), "click", function () {
                    view.graphics.removeAll();
                });
                this.tempWidget=null;
                this.layerList=null;
                view.ui.add("topbar", "top-left");
                view.ui.add("rightbar", "top-right");
                view.ui.add("baseMapDiv", "top-right");
                this.initMeasure(view);
                this.initListManager(view);
                this.initBaseMap(view);
            },
            setScaleBar: function (bool) {
                if (bool) {
                    this.scaleBar = new ScaleBar({
                        view: this.view,
                        unit: "metric"
                    });
                    this.view.ui.add(this.scaleBar, {
                        position: "bottom-left"
                    });
                } else if (this.scaleBar) {
                    this.view.ui.remove(this.scaleBar);
                    this.scaleBar = null;
                }
            },
            openPrint: function (bool) {
                if (bool) {
                    this.print = new Print({
                        view: this.view,
                        printServiceUrl: "http://localhost:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"
                    });

                    this.view.ui.add(this.print, "top-right");
                } else {
                    this.view.ui.remove(this.print);
                }
            },
            initListManager:function(view){
                var that=this;
                document.getElementById("layerButton").addEventListener("click",
                    function () {
                        if (!this.classList.contains('active')) {
                            this.classList.add("active");
                            that.layerList = new LayerList({
                                view: view
                            });
                            // 图层控件
                            view.ui.add(that.layerList, "top-right");
                        } else {
                            document.getElementById("layerButton").classList.remove("active");
                            view.ui.remove(that.layerList);
                            that.layerList.destroy();
                            that.layerList = null;
                        }
                    }
                );
            },
            initMeasure:function (view) {
                var activeWidget=this.tempWidget;
                document.getElementById("distanceButton").addEventListener("click",
                    function () {
                        setActiveWidget(null);
                        if (!this.classList.contains('active')) {
                            setActiveWidget('distance');
                        } else {
                            setActiveButton(null);
                        }
                    });

                document.getElementById("areaButton").addEventListener("click",
                    function () {
                        setActiveWidget(null);
                        if (!this.classList.contains('active')) {
                            setActiveWidget('area');
                        } else {
                            setActiveButton(null);
                        }
                    });

                function setActiveWidget(type) {
                    switch (type) {
                        case "distance":
                            activeWidget = new DistanceMeasurement2D({
                                view: view
                            });
                            activeWidget.viewModel.newMeasurement();
                            view.ui.add(activeWidget, "top-right");
                            setActiveButton(document.getElementById('distanceButton'));
                            break;
                        case "area":
                            activeWidget = new AreaMeasurement2D({
                                view: view
                            });
                            activeWidget.viewModel.newMeasurement();
                            view.ui.add(activeWidget, "top-right");
                            setActiveButton(document.getElementById('areaButton'));
                            break;
                        case null:
                            if (activeWidget) {
                                view.ui.remove(activeWidget);
                                activeWidget.destroy();
                                activeWidget = null;
                            }
                            break;
                    }
                }

                function setActiveButton(selectedButton) {
                    view.focus();
                    document.getElementById('distanceButton').classList.remove("active");
                    document.getElementById('areaButton').classList.remove("active");
                    if (selectedButton) {
                        selectedButton.classList.add("active");
                    }
                }
            },
            initBaseMap:function (view) {
                var that=this;
                document.getElementById("baseMapButton").addEventListener("click",
                    function () {
                        if (!this.classList.contains('active')) {
                            this.classList.add("active");
                            document.getElementById("baseMapDiv").style.display="block"

                        } else {
                            document.getElementById("baseMapButton").classList.remove("active");
                            document.getElementById("baseMapDiv").style.display="none"
                        }
                    }
                );
            }
        })
    });